#include "TCPServerContainer.h"
#include "Transmission/TCP/TCPServer.h"
#include "Transmission/UDP/UDPClient.h"
#include "Transmission/UDP/UDPGroup.h"
#include <QDebug>
#include <QDate>

TCPServer *server;
TCPClient *client;


TCPServerContainer::TCPServerContainer(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TCPServerContainerClass())
{
    ui->setupUi(this);
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &TCPServerContainer::Tick);
    timer->start(25);
    
    server = new TCPServer();
   
    server->RegisterClientConnectedCallback(&TCPServerContainer::OnServerClientConnected, this);
	server->RegisterClientDisconnectedCallback(&TCPServerContainer::OnServerClientDisconnected, this);
	server->RegisterClientReceivedCallback(&TCPServerContainer::OnServerClientReceived, this);

	/*client = new TCPClient();
	client->RegisterConnectCompletedCallback(&TCPServerContainer::OnConnectCompleted, this);
	client->RegisterDisconnectedCallback(&TCPServerContainer::OnDisconnected, this);
	client->RegisterReceivedCallback(&TCPServerContainer::OnReceived, this);*/

    on_StartButton_clicked();
    //new std::thread(&TCPServerContainer::testthread, this);
}

extern uint32_t CreateRef;
extern uint32_t DestroyRef;
TCPServerContainer::~TCPServerContainer()
{
    server->Terminate();
    client->Disconnect();
    qDebug() <<
        "c " << server->ConnectCount <<
        " d " << server->DisconnectCount <<
        " r " << server->ReceiveByteCount <<
        " s " << server->SendByteCount <<
        "||||" <<
        CreateRef << " "
        << DestroyRef;
    
    delete server;
    //delete client;
    disconnect(timer, &QTimer::timeout, this, &TCPServerContainer::Tick);
    delete ui;
  
}


void TCPServerContainer::OnServerClientConnected(TCPClient* client)
{
	const char* cmd = "START";
	int size = strlen(cmd);
	client->Send((char*)cmd, size);
    //client->Disconnect();
    //qDebug() << GetCurrentThreadId();
}

void TCPServerContainer::OnServerClientDisconnected(TCPClient* client)
{
    //qDebug() << GetCurrentThreadId();
    //client->Disconnect();
}


void TCPServerContainer::OnServerClientReceived(TCPClient* client, char* data, int len)
{
    //qDebug() << GetCurrentThreadId();
    client->Disconnect();
}




void TCPServerContainer::Tick()
{
    if (QDateTime::currentMSecsSinceEpoch() - lastts > 1000)
    {
        lastts = QDateTime::currentMSecsSinceEpoch();
		qDebug() <<
			"c " << server->ConnectCount <<
			" d " << server->DisconnectCount <<
			" r " << server->ReceiveByteCount <<
			" s " << server->SendByteCount <<
			"||||" <<
            CreateRef << " "
			<< DestroyRef;
    }
    server->Update();
    //client->Update();

    //on_StartButton_clicked();
}

void TCPServerContainer::on_StartButton_clicked()
{
    server->Initialize(5555);
    //client->AsyncConnect("127.0.0.1", 5555, 5000);
}

void TCPServerContainer::on_StopButton_clicked()
{
    server->Terminate();
    //client->Disconnect();
}


void TCPServerContainer::on_TestButton_clicked()
{
	const char* cmd = "START";
	int size = strlen(cmd);
    /*client->Send((char*)cmd, size);*/
    //udp->SendTo(cmd, size, "127.0.0.1", 51914);
}

void TCPServerContainer::OnConnectCompleted(int code, std::string error)
{
	const char* cmd = "START";
	int size = strlen(cmd);
	client->Send((char*)cmd, size);
	//client->Disconnect();
}

void TCPServerContainer::OnDisconnected()
{

}

void TCPServerContainer::OnReceived(char* data, int len)
{
	QByteArray ba(data, len);
	qDebug() << ba;
}
